package classes.view.component
{
	import classes.controller.ENumber;
	
	import com.d4p.custom.defaultButton.DefaultBtn;
	
	import flash.display.Sprite;
	import flash.events.MouseEvent;

	public class PopupPagingContainer extends PagingContainer
	{
		private var _nextBtn_popup:DefaultBtn;
		private var _prevBtn_popup:DefaultBtn;
		
		
		public function PopupPagingContainer()
		{
			super();
			_pageNumDistance = 18;
			setLayOut();
			init();
		}
		override protected function setLayOut():void
		{
			this._pageArr	= new Array();
			
			this._nextBtn_popup 	= this.getChildByName('next_btn_pop')	as DefaultBtn;
			this._prevBtn_popup 	= this.getChildByName('prev_btn_pop')	as DefaultBtn;
			
			this._paging	= new Sprite();
			this.addChild(this._paging);
		}
		
		override protected function onClickHandler(e:MouseEvent):void
		{
			switch(e.currentTarget){
				case _prevBtn_popup :
					//trace('prev');
					dispatchEvent(new ENumber(_beforePage-1, ENumber.EVENT_NUMBER));
					break;
				case _nextBtn_popup :
					//trace('next');
					dispatchEvent(new ENumber(_beforePage+1, ENumber.EVENT_NUMBER));
					break;
			}
		}
		
		override protected function setPageLayOut($pageNum:int, $totalNum:int):void
		{
			if(this._pageArr == null) this._pageArr	= new Array();
			var i:int = 1 ;
			
			if($totalNum < 9){ //토탈이 9 미만일 경우.
				
				for(i = 1 ; i <= $totalNum ; i++){
					
					this._pageArr[i-1] 				= new PagingNumber_popup();
					this._pageArr[i-1].pagingNum 	= i;
					this._pageArr[i-1].x			= _pageNumDistance*(i-1);
					this._paging.addChild(this._pageArr[i-1]);
					this._pageArr[i-1].addEventListener(MouseEvent.CLICK,	onClickPaging);
				}
				this._paging.x = ((_pageNumDistance*9)-(_pageNumDistance*$totalNum))/2;
				this._pageArr[$pageNum-1].selectPage 		= true;
			}else{
				//trace("A");
				if($pageNum < 5){
					for(i = 1 ; i < 10 ; i++){
						this._pageArr[i-1] 				= new PagingNumber_popup();
						this._pageArr[i-1].pagingNum 	= i;
						this._pageArr[i-1].x			= _pageNumDistance*(i-1);
						this._paging.addChild(this._pageArr[i-1]);
						this._pageArr[i-1].addEventListener(MouseEvent.CLICK,	onClickPaging);
					}
					this._pageArr[$pageNum-1].selectPage 		= true;
					
				}else if($pageNum <= $totalNum-4){
					
					for(i = 1 ; i < 10 ; i++){
						this._pageArr[i-1] 				= new PagingNumber_popup();
						this._pageArr[i-1].pagingNum 	= $pageNum+((i-1)-4);
						this._pageArr[i-1].x			= _pageNumDistance*(i-1);
						this._paging.addChild(this._pageArr[i-1]);
						this._pageArr[i-1].addEventListener(MouseEvent.CLICK,	onClickPaging);
					}
					this._pageArr[4].selectPage 		= true;
					
				}else{
					//trace("B");
					for(i = 1 ; i < 10 ; i++){
						this._pageArr[i-1] 				= new PagingNumber_popup();
						this._pageArr[i-1].pagingNum 	= $totalNum + ((i-1)-8);
						this._pageArr[i-1].x			= _pageNumDistance*(i-1);
						this._paging.addChild(this._pageArr[i-1]);
						this._pageArr[i-1].addEventListener(MouseEvent.CLICK,	onClickPaging);
					}
					
					var arr:Array = [4,3,2,1];
					this._pageArr[4+arr[($totalNum-$pageNum)]].selectPage 		= true;
				}
				this._paging.x = 0;
			}
		}
		override protected function pagingSetup($pageNum:int, $totalNum:int):void
		{
			if($pageNum == $totalNum){
				_nextBtn_popup.alpha = 0.3;
				_nextBtn_popup.mouseEnabled  = false;
				_nextBtn_popup.mouseChildren = false;
			}else{
				_nextBtn_popup.alpha = 1;
				_nextBtn_popup.mouseEnabled  = true;
				_nextBtn_popup.mouseChildren = true;
			}
			if($pageNum == 1){
				_prevBtn_popup.alpha = 0.3;
				_prevBtn_popup.mouseEnabled  = false;
				_prevBtn_popup.mouseChildren = false;
			}else{
				_prevBtn_popup.alpha = 1;
				_prevBtn_popup.mouseEnabled  = true;
				_prevBtn_popup.mouseChildren = true;
			}
		}
		
		
		override protected function init():void
		{
			_nextBtn_popup.addEventListener(MouseEvent.CLICK,		onClickHandler);
			_prevBtn_popup.addEventListener(MouseEvent.CLICK,		onClickHandler);
		}
		
		override protected function onClickPaging(e:MouseEvent):void
		{
			dispatchEvent(new ENumber(e.currentTarget.pagingNum, ENumber.EVENT_NUMBER));
		}
		
	}
}